package cn.edu.sdjzu.xg23.kcsj01_backend.service.impl;


import cn.edu.sdjzu.xg23.kcsj01_backend.entity.ActorType;
import cn.edu.sdjzu.xg23.kcsj01_backend.entity.Employee;
import cn.edu.sdjzu.xg23.kcsj01_backend.entity.User;
import cn.edu.sdjzu.xg23.kcsj01_backend.mapper.ActorMapper;
import cn.edu.sdjzu.xg23.kcsj01_backend.mapper.EmployeeMapper;
import cn.edu.sdjzu.xg23.kcsj01_backend.mapper.UserMapper;
import cn.edu.sdjzu.xg23.kcsj01_backend.service.EmployeeService;
import cn.edu.sdjzu.xg23.kcsj01_backend.service.UserService;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.PinyinUtil;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.SnowflakeIdGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;
    @Autowired
    private User user;
    @Autowired
    private UserMapper  usermapper;
    @Autowired
    private ActorMapper actorMapper;
    @Override
    @Transactional
    public boolean addEmployee(Employee employee) {
        Long sharedId = snowflakeIdGenerator.generateId();
        actorMapper.addActor(sharedId); // 添加 actor
        employee.setId(sharedId);

        user.setUsername(PinyinUtil.getFullPinyin(employee.getName()));//  用户名 由员工姓名拼音全拼字母 组成
        user.setPassword(PinyinUtil.getInitials(employee.getName())+"123"); // 密码 由员工姓名拼音首字母+123 组成
        user.setActorType(ActorType.TEACHER);
        user.setActor(employee);
        user.setId(snowflakeIdGenerator.generateId());
        usermapper.addUser(user);

        //这里暂未设置department

        employee.setUser(user);
        if (employeeMapper.addEmployee(employee)){
            return true;
        }
        return false;
    }

    @Override
    public PageInfo<Employee> get(int pageNum, int pageSize, Employee employee){
        PageHelper.startPage(pageNum,pageSize);
        List<Employee> employeesList  = employeeMapper.get(employee);
        return PageInfo.of(employeesList);
    }



    public String save(Employee employee){
        if( employee.getId() == null){
            Long id = snowflakeIdGenerator.generateId();
            employee.setId(id);
        }


        if(employeeMapper.save(employee)){
            return "保存成功";
        }
        return "保存失败";
    }
    public String update(Employee employee){
        if(employeeMapper.update(employee)){
            return "修改成功";
        }
        return "修改失败";
    }
    public String delete(long id){
        if(employeeMapper.delete(id)){
            return "删除成功";
        }
        return "删除失败";
    }
}
